
from funcall.AbstractFunCall import AbstractFunCall, fun_call_log_debug, FUN_CALL_REMARK

from conf.config import AppConf
from funcall.FuncallInfo import FuncallInfo
from funcalltools.file_utils import fc_write_file
from funcall.funcall_result import FuncallResult
from utils.log_utils import log_title


class file_editing_fun_call(AbstractFunCall):
    def __init__(self):
        self.name = "file_editing"
        self.description = "创建或编辑文件"

        work_dir = AppConf['ai']['work_dir']
        # 因为获取当前时间无需输入参数，因此parameters为空字典
        self.parameters = {
            "type": "object",
            "properties": {
                "filename": {
                    "type": "string",
                    "description": f"文件名称,使用文件名全路径,你允许操作的目录是 {work_dir}"
                },
                "content": {
                    "type": "string",
                    "description": "要设置的文件内容,调用后如果无错误信息则说明保存成功"
                },
                "remark": {
                    "type": "string",
                    "description": FUN_CALL_REMARK
                }
            }
        }
        self.required = [
            "remark",
            "filename",
            "content"
        ]

    def action(self, jsonRoot):
        content_tmp = "" if 'content' not in jsonRoot else jsonRoot['content']
        filename_tmp = jsonRoot['filename']
        funcallInfo=FuncallInfo(description="file_editing", req_args=filename_tmp, log_text=content_tmp)
        funcallInfo.remark=jsonRoot['remark']
        fun_call_log_debug(funcallInfo)
        fc_write_file(filename_tmp, content_tmp)
        return FuncallResult(msg="成功").to_json()


if __name__ == '__main__':
    import logging

    print("setting logging level")
    logging.basicConfig(level=logging.NOTSET)
    logging.debug("asus")
    # 创建一个GetCurrentWeatherFunCall对象
    cw = file_editing_fun_call()

    # 调用GetCurrentWeatherFunCall对象的call方法
    args = {"filename": "Z:/aiwork/a.text"}
    result = cw.call(args)
    result = cw.call("""{"filename":"Z:/aiwork/a.text"}""")

    # 打印结果
    print(result)
